﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows;
using System.Xml.Linq;

namespace ThunderboltCustomControlLibrary;


	[DefaultProperty("MenuItems")]
	[ContentProperty("MenuItems")]
	[TemplatePart(Name = LB, Type = typeof(ListBox))]
	public class SideMenuItem : Control
	{
		private const string LB = "LB";
		private ListBox _listBox;

	static SideMenuItem()
	{
		DefaultStyleKeyProperty.OverrideMetadata(typeof(SideMenuItem), new FrameworkPropertyMetadata(typeof(SideMenuItem)));
	}
	public bool IsExpanded
		{
			get { return (bool)GetValue(IsExpandedProperty); }
			set { SetValue(IsExpandedProperty, value); }
		}
		public static readonly DependencyProperty IsExpandedProperty =
			DependencyProperty.Register("IsExpanded", typeof(bool), typeof(SideMenuItem), new PropertyMetadata(false));

	
		
		public Geometry Icon
		{
			get { return (Geometry)GetValue(IconProperty); }
			set { SetValue(IconProperty, value); }
		}

		// Using a DependencyProperty as the backing store for Icon.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty IconProperty =
			DependencyProperty.Register("Icon", typeof(Geometry), typeof(SideMenuItem));


	//todo:IconPathValueButton替换为Object不适用
	public ObservableCollection<IconPathValueButton> MenuItems
		{
			get { return (ObservableCollection<IconPathValueButton>)GetValue(MenuItemsProperty); }
			set { SetValue(MenuItemsProperty, value); }
		}
		public static readonly DependencyProperty MenuItemsProperty =
			DependencyProperty.Register("MenuItems", typeof(ObservableCollection<IconPathValueButton>), typeof(SideMenuItem), new PropertyMetadata(default(ObservableCollection<IconPathValueButton>)));


		public Brush ToggleBackground
		{
			get { return (Brush)GetValue(ToggleBackgroundProperty); }
			set { SetValue(ToggleBackgroundProperty, value); }
		}
		public static readonly DependencyProperty ToggleBackgroundProperty =
			DependencyProperty.Register("ToggleBackground", typeof(Brush), typeof(SideMenuItem), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(0x2d, 0x2d, 0x30))));


		public Brush MenuItemBackground
		{
			get { return (Brush)GetValue(MenuItemBackgroundProperty); }
			set { SetValue(MenuItemBackgroundProperty, value); }
		}
		public static readonly DependencyProperty MenuItemBackgroundProperty =
			DependencyProperty.Register("MenuItemBackground", typeof(Brush), typeof(SideMenuItem), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(0x16, 0x18, 0x1D))));


		public string HeaderText
		{
			get { return (string)GetValue(HeaderTextProperty); }
			set { SetValue(HeaderTextProperty, value); }
		}
		public static readonly DependencyProperty HeaderTextProperty =
			DependencyProperty.Register("HeaderText", typeof(string), typeof(SideMenuItem), new PropertyMetadata(string.Empty));




	public Brush MenuItemSelectedBackground
		{
			get { return (Brush)GetValue(MenuItemSelectedBackgroundProperty); }
			set { SetValue(MenuItemSelectedBackgroundProperty, value); }
		}
		public static readonly DependencyProperty MenuItemSelectedBackgroundProperty =
			DependencyProperty.Register("MenuItemSelectedBackground", typeof(Brush), typeof(SideMenuItem), new PropertyMetadata(Brushes.Green));


		public static readonly RoutedEvent MenuItemSelectedChangedEvent = EventManager.RegisterRoutedEvent("MenuItemSelectedChanged", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(SideMenuItem));


		public event RoutedEventHandler MenuItemSelectedChanged
		{
			add { AddHandler(MenuItemSelectedChangedEvent, value); }
			remove { RemoveHandler(MenuItemSelectedChangedEvent, value); }
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			_listBox = (GetTemplateChild(LB) as ListBox) ?? throw new Exception("listbox Named with \"LB\" not found in the Template");
			_listBox.SelectionChanged -= _listBoxSelectionChanged;
			_listBox.SelectionChanged += _listBoxSelectionChanged;
		}


		private void _listBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (!(e.Source is ListBox)) return;
			RoutedEventArgs args = new RoutedEventArgs()
			{
				RoutedEvent = MenuItemSelectedChangedEvent,
				Source = _listBox,
			};
			RaiseEvent(args);
		}
	}

